package cn.appsys.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.List;

@Configuration
@EnableWebMvc //相当于开启注解方式
@ComponentScan(value = "cn.appsys.controller")
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("backendlogin.jsp");
    }

    @Override //视图解析器 注意：views文件夹在resource下
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/classes/views/", null);
    }

    @Override //静态资源映射 注意：static文件夹在resource下
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

    @Override  //json前台到后台的问题：中文编码和日期问题
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setCharset(Charset.forName("utf-8"));
        fastJsonHttpMessageConverter.setFeatures(SerializerFeature.WriteDateUseDateFormat);
        converters.add(fastJsonHttpMessageConverter);
    }

    @Override //注册 ：前台到后台的日期问题
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringFormatByDate());
    }

    @Override //配置拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }
}
